<?php

//后台的配置文件
include(dirname(__FILE__).'/config.php');
include(dirname(__FILE__).'/../Inc/tree.class.php');



$dopost = isset($_REQUEST['dopost']) ? trim($_REQUEST['dopost']) : 'list';


/***************************************************************************/
if( 'back' == $dopost ){
	$id = isset($_GET['id']) ? intval($_GET['id']) : 0 ;

	if( !$id ){ die('{"ret":999,"msg":"id错误!!!"}'); }

	$art = array();
	$art['recycle'] =  0 ;
	$rs = dbUpdate('article',$art," id = '{$id}'"); // 表 数据 条件
	
	if($rs){
		$j = array();
		$j['ret'] = 0 ;
		$j['msg'] = '撤销删除成功!!!';
		//$j['url'] = '?tid='.$art['tid'].'&t='.time() ;
		die( json_encode($j) );

	}else{
		$j = array();
		$j['ret'] = 999 ;
		$j['msg'] = '撤销删除失败!!!';
		die( json_encode($j) );
	}



/***************************************************************************/
}else if( 'del' == $dopost ){
	$id = isset($_GET['id']) ? intval($_GET['id']) : 0;

	//真删除
	$rs = dbDelete('article'," recycle=1 and id = '{$id}'"); // 表 数据 条件

	if($rs){
		$j = array();
		$j['ret'] = 0 ;
		$j['msg'] = '删除成功!!!';
		//$j['url'] = '?tid='.$art['tid'].'&t='.time() ;
		die( json_encode($j) );

	}else{
		$j = array();
		$j['ret'] = 999 ;
		$j['msg'] = '删除失败!!!';
		die( json_encode($j) );
	}
/***************************************************************************/
}else if( 'batch_del' == $dopost ){
	$id_arr = $_POST['id'];
	
	foreach($id_arr as $k => $v){
		//真删除
		$rs = dbDelete('article'," recycle=1 and id = '{$k}'"); // 表 数据 条件
	}
	
	die('{"ret":0,"msg":"批量 【删除】 成功!!!"}');
	

/***************************************************************************/
}else if( 'batch_back' == $dopost ){
	$id_arr = $_POST['id'];
	
	foreach($id_arr as $k => $v){
		$art = array();
		$art['recycle'] =  0 ;
		$rs = dbUpdate('article',$art," id = '{$k}'"); // 表 数据 条件
	}
	
	die('{"ret":0,"msg":"批量 【撤销】 成功!!!"}');

	

/***************************************************************************/
}else if( 'list' == $dopost ){

	$tid = isset($_GET['tid']) ? intval($_GET['tid']):0;  //  1
	$s = isset($_GET['s']) ? trim($_GET['s']):'';

	//条件过来 是否 有 tid
    if( $tid ){

    	$typelist = dbList("select * from `type` where 1 order by sort asc , tid asc ");
		$tree = new tree();//类实例化为对象
		$tree->init( $typelist );//初始化数据  
		$ids =    $tree->getIds( $tid );

        $where_tid = "and a.tid in ({$ids}) ";
    }else{
        $where_tid = '';
    }
    if( $s ){
        $where_s = "and a.title like '%{$s}%'";
    }else{
        $where_s = '';
    }
    


    $sql = "select a.*  , b.typename   
    from `article` as a left join `type` as b on a.tid = b.tid 
    where 1 and a.recycle=1  $where_tid  {$where_s}
    order by sort asc ,id desc ";


    //var_dump($sql);

    $dl = new ListPage();
    $dl->pageSize = 50;//每页显示多少条记录
    $dl->SetParameter('tid',$tid); //向 页码url中添加其他参数
    $dl->SetParameter('s',$s); //向 页码url中添加其他参数
    $dl -> SetSql($sql);
    $list = $dl ->GetList(); //获取 list 数组2
    $page = $dl ->GetPage(); //获取 分页代码*/


	include('temp/recycle_list.php');
/***************************************************************************/
}else{
	die('404');
}
/***************************************************************************/


function typelistOption( $select_id = 0 ){

	$typelist = dbList("select * from `type` where 1 order by sort asc , tid asc ");
	$tree = new tree();//类实例化为对象
	$tree->init( $typelist );//初始化数据  
	$typelist =    $tree->get_tree_list( 0 , $select_id ,'' );
	$html = '';
	foreach($typelist as $k => $v ){
		$html .=" <option value=\"{$v['tid']}\"  {$v['_selected']} > {$v['_prefix']}{$v['typename']}</option> ";
	}
	return $html;
}